package com.jhsw.roadsun.module.business.config.convert;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.idev.excel.converters.Converter;
import cn.idev.excel.metadata.GlobalConfiguration;
import cn.idev.excel.metadata.data.WriteCellData;
import cn.idev.excel.metadata.property.ExcelContentProperty;

import java.io.IOException;
import java.net.URL;

/**
 * @author Ysugar
 */
public class UrlStrImageConverter implements Converter<String> {

    public static int urlConnectTimeout = 1000;
    public static int urlReadTimeout = 5000;

    @Override
    public Class<?> supportJavaTypeKey() {
        return URL.class;
    }

    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)
            throws IOException {

        if (StrUtil.isBlank(value))
            return new WriteCellData<>();

        return new WriteCellData<>(HttpUtil.downloadBytes(value));
    }
}
